package com.mf.config;

import org.hibernate.cfg.ImprovedNamingStrategy;

public class CustomNamingStrategy extends ImprovedNamingStrategy {

	private static final long serialVersionUID = 1L;
	public static final String TABLE_PREFIX = "PB_";
	public static final String FOREIGN_COLUMN_SUFFIX = "_RID";

	@Override
	public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName,
			String referencedColumnName) {
		String foreignKeyColumnName = super.foreignKeyColumnName(propertyName, propertyEntityName, propertyTableName,
				referencedColumnName);

		return addForeignColumnSuffix(foreignKeyColumnName);
	}

	@Override
	public String classToTableName(final String className) {
		return this.addTablePrefix(super.classToTableName(className));
	}

	@Override
	public String collectionTableName(final String ownerEntity, final String ownerEntityTable,
			final String associatedEntity, final String associatedEntityTable, final String propertyName) {
		return this.addTablePrefix(super.collectionTableName(ownerEntity, ownerEntityTable, associatedEntity,
				associatedEntityTable, propertyName));
	}

	@Override
	public String logicalCollectionTableName(final String tableName, final String ownerEntityTable,
			final String associatedEntityTable, final String propertyName) {
		return this.addTablePrefix(super.logicalCollectionTableName(tableName, ownerEntityTable, associatedEntityTable,
				propertyName));
	}

	private String addTablePrefix(final String composedTableName) {
		return TABLE_PREFIX + composedTableName.toUpperCase();
	}

	private String addForeignColumnSuffix(final String foreignColumnName) {
		return foreignColumnName.toUpperCase() + FOREIGN_COLUMN_SUFFIX;
	}
}
